package com.ruoyi.web.controller.system;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;


@Controller
@RequestMapping("/bus/audit")
public class BusExportController {



    @RequiresPermissions("bus:audit:templateExport")
    @RequestMapping("/templateExport")

    public String list()
    {
        return "bus/audit/templateExport.html";
    }

    @RequiresPermissions("bus:audit:exportExport")
    @RequestMapping("/exportExport")
    @ResponseBody
    public String exportExport(HttpServletRequest request , HttpServletResponse response) throws IOException {

            String [] ele_name= request.getParameterValues("ele_name");
            String []  factor=request.getParameterValues("factor");

       //创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        //建立新的sheet对象（excel的表单）
        HSSFSheet sheet=wb.createSheet("约修文档");
        //在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
        HSSFRow row1=sheet.createRow(0);
        //创建单元格（excel的单元格，参数为列索引，可以是0～255之间的任何一个
        HSSFCell cell=row1.createCell(0);
        //设置单元格内容
        cell.setCellValue("请按内容填写");
        //合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
        //在sheet里创建第二行

        HSSFRow row2=sheet.createRow(1);
            for( int i=0;i<factor.length;i++){
                row2.createCell(i+2).setCellValue(factor[i].toString());
            }
            row2.createCell(1).setCellValue("监测类别");
             row2.createCell(factor.length+1).setCellValue("数据类型");
            row2.createCell(factor.length+2).setCellValue("时间日期");
        //在sheet里创建后续行
            for(int i=0;i<ele_name.length;i++){
                HSSFRow row=sheet.createRow(i+2);
                row.createCell(0).setCellValue(ele_name[i].toString());
            }
       // 输出Excel文件
        OutputStream output=response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=details.xls");
        response.setContentType("application/msexcel");
        wb.write(output);
        output.close();

        return  "导出成功";
    }



    @RequiresPermissions("bus:audit:importExport")
    @RequestMapping("/importExport")
    @ResponseBody
    public String importexport(HttpServletResponse response , HttpServletRequest request){


        return  "成功";
    }



//    @ResponseBody
//    @RequestMapping("/uploadImg.html")
//    public String uploadPicture(
//            @RequestParam(value="file",required=false) MultipartFile file,
//            HttpServletRequest request){
//        File targetFile=null;
//        String msg="";//返回存储路径
//        int code=1;
//        String fileName=file.getOriginalFilename();//获取文件名加后缀
//        if(fileName!=null&&fileName!=""){
//            //String returnUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() +"/upload/excel/";//存储路径
//            String path = request.getSession().getServletContext().getRealPath("upload/excel"); //文件存储位置
//            String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀
//            fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;//新的文件名
//
//            //先判断文件是否存在
//            String fileAdd = DateUtil.format(new Date(),"yyyyMMdd");
//            File file1 =new File(path+"/"+fileAdd);
//            //如果文件夹不存在则创建
//            if(!file1 .exists()  && !file1 .isDirectory()){
//                file1 .mkdir();
//            }
//            targetFile = new File(file1, fileName);
//            //        targetFile = new File(path, fileName);
//            try {
//                file.transferTo(targetFile);
//                //        	msg=returnUrl+fileName;
//                //        	msg=returnUrl+fileAdd+"/"+fileName;
//                msg=path+"/"+fileAdd+"/"+fileName;
//                code=0;
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return JSON.toJSONString(ResponseResult.result(code, msg));
//    }


}
